<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell>
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        round
        plain
        size="small"
        type="danger"
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <!-- 我的频道宫格 -->
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in myDataChannels"
        :key="index"
        @click="onMyChannelClick(channel, index)"
      >
        <span slot="text" class="text" :class="{ active: index === active }">{{
          channel.name
        }}</span>
        <van-icon
          v-show="isEdit && !fiexdChannels.includes(channel.id)"
          name="clear"
          slot="icon"
        />
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐 -->
    <van-cell>
      <div slot="title">频道推荐</div>
    </van-cell>
    <!-- 频道推荐宫格 -->
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        icon="plus"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { mapState } from "vuex";
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel";
import { setItem } from "@/utils/storage";
export default {
  name: "ChannelEdit",
  created() {
    this.loadAllChannels();
  },
  data() {
    return {
      isEdit: false,
      allChannels: [],
      myDataChannels: this.myChannels,
      fiexdChannels: [0],
    };
  },
  props: {
    myChannels: {
      type: Array,
      required: true,
    },
    active: {
      type: Number,
      required: true,
    },
  },
  methods: {
    // 获取所有频道列表
    async loadAllChannels() {
      const { data } = await getAllChannels();
      console.log(data);
      this.allChannels = data.data.channels;
    },
    // 添加频道事件句柄
    async onAddChannel(channel) {
      this.myDataChannels.push(channel);
      if (this.user) {
        try {
          // 登录状态
          await addUserChannel({
            id: channel.id,
            seq: this.myDataChannels.length,
          });
        } catch (e) {
          this.$toast("保存失败");
        }
      } else {
        // 非登录状态存本地
        setItem("TOUTIAO_CHANNELS", this.myDataChannels);
      }
    },
    // 点击我的频道事件句柄
    onMyChannelClick(channel, index) {
      // 如果处于
      if (this.isEdit) {
        // 固定频道不进行操作
        if (this.fiexdChannels.includes(index)) {
          return;
        }
        // 编辑状态，删除频道
        this.myDataChannels.splice(index, 1);
        // 如果删除活跃频道之前的频道，活跃索引减一，并不关闭弹窗
        if (index < this.active) {
          this.$emit("update-active", this.active - 1, true);
        }

        this.deleteChannel(channel);
      } else {
        // 非编辑状态，切换频道
        this.$emit("update-active", index, false);
      }
    },
    async deleteChannel(channel) {
      if (this.user) {
        // 登录状态
        try {
          await deleteUserChannel(channel.id)
        } catch (e) {
          this.$toast('删除失败,请稍后重试')
        }
      } else {
        // 非登录状态存本地
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },
  },
  computed: {
    recommendChannels() {
      // 过滤掉myChannels
      return this.allChannels.filter((item) => {
        // 找到属于myChannels的item
        return !this.myChannels.find((channel) => {
          return channel.id === item.id;
        });
      });
    },
    ...mapState(["user"]),
  },
};
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    line-height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;

    .van-icon-clear {
      position: absolute;
      right: -10px;
      top: -10px;
      font-size: 30px;
      color: #cacaca;
      z-index: 2;
    }
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__icon-wrapper {
        position: unset;
      }
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
      }
      .active {
        color: red;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;

        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          margin-top: 0;
        }
      }
    }
  }
}
</style>